package com.menggudf.controller;

import com.menggudf.entity.User;
import com.menggudf.model.ResultModel;
import com.menggudf.model.ResultStatus;
import com.menggudf.model.UserModel;
import com.menggudf.service.UserService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2025-02-20 16:58:34
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;



    /**
     * 分页查询
     *
     * @param user 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<User>> queryByPage(User user, PageRequest pageRequest) {
        return ResponseEntity.ok(this.userService.queryByPage(user, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<User> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.userService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param user 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<User> add(User user) {
        return ResponseEntity.ok(this.userService.insert(user));
    }

    /**
     * 编辑数据
     *
     * @param user 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<User> edit(User user) {
        return ResponseEntity.ok(this.userService.update(user));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.userService.deleteById(id));
    }

    @RequestMapping("/login")
    @ResponseBody
    public ResultModel login(HttpServletRequest request,
                             HttpServletResponse response,
                             @RequestParam("phone") String phone,
                             @RequestParam("psw") String passWord,
                             HttpSession httpSession){
        ResultModel model =userService.userLogin(phone, passWord);
        if(model.getResultCode()== ResultStatus.RESULT_STATUS_SUCCESS.getValue()){
            UserModel userModel = (UserModel) model.getContent();
            httpSession.setAttribute("userModel", userModel);
        }
        return model;

    }

    /**
     * 退出登录
     * @param request
     * @param response
     * @param httpSession
     * @return
     */
    @RequestMapping("/loginOut")
    @ResponseBody
    public ResultModel loginOut(HttpServletRequest request,
                                HttpServletResponse response,HttpSession httpSession){
        if(httpSession.getAttribute("userModel")!=null){
            httpSession.removeAttribute("userModel");
        }
        if(httpSession.getAttribute("userModel")==null){
            return ResultModel.success();
        }else{
            return ResultModel.fail();
        }
    }

    /**
     * 获取用户列表
     * @return
     */
    @RequestMapping("/getUserList")
    @ResponseBody
    public Map<String, Object> getUserList(
            @RequestParam(value="userCode",required=false) String userCode,
            @RequestParam(value="userName",required=false) String userName,
            @RequestParam("limit") int pageSize,
            @RequestParam("offset") int offset,
            @RequestParam(value="startTime",required=false) Date startTime,
            @RequestParam(value="endTime",required=false) Date endTime){
        return userService.getUserList(userCode,userName,pageSize,offset,startTime,endTime);

    }

    @RequestMapping("/getUserDetail")
    @ResponseBody
    public UserModel getUserDetail(
            @RequestParam(value="userCode",required=false) String userCode){
        return userService.getUserDetail(userCode);

    }

}

